package o2o.web.actions.authrole;

import com.jinyou.utils.common.AjaxResult;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.dao.PO.authrole.RoleInfo;
import o2o.dao.PO.company.SysCityCompanyInfo;
import o2o.dao.PO.user.UserInfo;
import o2o.dao.VO.authrole.AuthInfoVO;
import o2o.dao.VO.authrole.RoleInfoVO;
import o2o.dao.VO.authrole.UserRoleAuthInfoVO;
import o2o.web.actions.company.SysCityCompanyMan;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 *
 * @User: @Created by yangtk
 * @Date: @Date 2019/7/15 16:04
 * @Classname: AuthRoleController
 * @To change this template use File | Settings | File Templates.
 */
@Controller
public class AuthRoleController {

    // **********************************公司角色信息**********************************

    // 创建公司的角色信息
    @RequestMapping(value = "admin/role/create", method = RequestMethod.POST)
    @ResponseBody
    public String createRole(String token, String auths, RoleInfo info) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        if (StringUtils.isEmpty(info.getRoleName())) {
            return AjaxResult.errorResult("角色名不能为空");
        }
        SysCityCompanyInfo companyInfo = SysCityCompanyMan.getByCompanyUsername(user.getUsername());
        if (companyInfo == null) {
            return AjaxResult.errorResult("未获取到公司信息");
        }
        UserRoleAuthInfoVO authInfoVO = AuthRoleMan.getUserRoleAuthRoleByUsername(user.getUsername(), user.getUserType());
        if (authInfoVO == null) {
            return AjaxResult.errorResult("未获取权限信息");
        }
        try {
            // 创建一个角色信息
            AuthRoleMan.createRole(auths, user.getUsername(), info.setCompanyId(companyInfo.getId()), authInfoVO.getAuthInfoList2());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult(e.getMessage());
        }
        return AjaxResult.successResult();
    }

    // 获取公司创建角色信息
    @RequestMapping(value = "admin/role/list", method = RequestMethod.POST)
    @ResponseBody
    public String getRoleList(String token, String username) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        if (StringUtils.isEmpty(username)) {
            username = user.getUsername();
        }
        if (StringUtils.isEmpty(username)) {
            return AjaxResult.errorResult("账号信息不能为空");
        }

        SysCityCompanyInfo companyInfo = SysCityCompanyMan.getByCompanyUsername(user.getUsername());
        if (companyInfo == null) {
            return AjaxResult.errorResult("未获取到公司信息");
        }
        List<RoleInfoVO> roleInfoVOS = AuthRoleMan.getCreateListOfUserRoleInfoByCompanyId(companyInfo.getId());
        if (roleInfoVOS != null) {
            return AjaxResult.successResult(roleInfoVOS.size(), roleInfoVOS);
        }
        return AjaxResult.successResult();
    }

    // 删除公司创建的角色信息
    @RequestMapping(value = "admin/role/del", method = RequestMethod.POST)
    @ResponseBody
    public String delRole(String token, String username, Long roleId) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        if (StringUtils.isEmpty(username)) {
            username = user.getUsername();
        }
        if (StringUtils.isEmpty(username)) {
            return AjaxResult.errorResult("账号信息不能为空");
        }
        if (roleId == null) {
            return AjaxResult.errorResult("角色id不能为空");
        }

        if (!AuthRoleMan.checkRoleIdIsInUserCompany(roleId, username)) {
            return AjaxResult.errorResult("该角色不属于您");
        }
        Set<Long> roleIdSet = new HashSet<>();
        roleIdSet.add(roleId);
        // 删除
        try {
            AuthRoleMan.delUserCreateRole(roleIdSet, username);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult(e.getMessage());
        }
        return AjaxResult.successResult();
    }

    // 更新公司的角色信息
    @RequestMapping(value = "admin/role/modify", method = RequestMethod.POST)
    @ResponseBody
    public String modifyRole(String token, String auths, RoleInfo info) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        if (info.getId() == null) {
            return AjaxResult.errorResult("角色id不能为空");
        }
        UserRoleAuthInfoVO roleAuthInfoVO = AuthRoleMan.getUserRoleAuthRoleByUsername(user.getUsername(), user.getUserType());
        if (roleAuthInfoVO == null) {
            return AjaxResult.errorResult("未获取到用户角色权限信息");
        }
        try {
            AuthRoleMan.updateUserCreateRole(auths, info, roleAuthInfoVO.getAuthInfoList2(), user.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult(e.getMessage());
        }
        return AjaxResult.successResult();
    }

    // 获取当前的用户拥有的权限(包含角色拥有的)
    @RequestMapping(value = "/admin/auth/list")
    @ResponseBody
    public String listOfAuthByUsername(String token) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        List<AuthInfoVO> authInfoVOList = AuthRoleMan.listOfAuthInfoByUsername(user.getUsername(), user.getUserType());
        if (authInfoVOList != null) {
            return AjaxResult.successResult(authInfoVOList.size(), authInfoVOList);
        }
        return AjaxResult.successResult();
    }

    // 获取用户的权限 给谁授权
    @RequestMapping(value = "/admin/auth/list/all")
    @ResponseBody
    public String listOfAuthByUsernameAll(String token, String authType) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        UserRoleAuthInfoVO authRoleByUsername = AuthRoleMan.getUserRoleAuthRoleByUsername(user.getUsername(), authType, user.getUserType());
        if (authRoleByUsername != null && authRoleByUsername.getAuthInfoList2() != null) {
            return AjaxResult.successResult(authRoleByUsername.getAuthInfoList2().size(), authRoleByUsername.getAuthInfoList2());
        }
        return AjaxResult.successResult();
    }


    //***********************************用户拥有的权限信息相关授权**************************************************

    // 给用户授权
    @RequestMapping(value = "admin/role/auth/put", method = RequestMethod.POST)
    @ResponseBody
    public String modifyRole(String token, String toUsername, String auths, String roles) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        if (StringUtils.isEmpty(toUsername)) {
            return AjaxResult.errorResult("被授权人不能为空");
        }
        UserRoleAuthInfoVO roleAuthInfoVO = AuthRoleMan.getUserRoleAuthRoleByUsername(user.getUsername(), user.getUserType());
        if (roleAuthInfoVO == null) {
            return AjaxResult.errorResult("获取用户权限信息失败");
        }
        SysCityCompanyInfo companyInfo = SysCityCompanyMan.getByCompanyUsername(user.getUsername());
        if (companyInfo == null) {
            return AjaxResult.errorResult("未获取到公司信息");
        }
        List<RoleInfoVO> roleInfoVOS = AuthRoleMan.getCreateListOfUserRoleInfoByCompanyId(companyInfo.getId());
        try {
            AuthRoleMan.authOrRuleToUsername(toUsername, auths, roles, roleAuthInfoVO.getAuthInfoList2(), roleInfoVOS, user.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult(e.getMessage());
        }
        return AjaxResult.successResult();
    }


    // 获取用户的角色和权限信息
    @RequestMapping(value = "admin/role/auth/get", method = RequestMethod.POST)
    @ResponseBody
    public String modifyRole(String token, String username) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        int type = user.getUserType();
        if (StringUtils.isEmpty(username)) {
            username = user.getUsername();
        } else {
            UserInfo userInfo = UserInfoMemory.getInstance().getUserInfo(username);
            if (userInfo == null) {
                return AjaxResult.errorResult("未获取到用户信息");
            }
            type = userInfo.getUserType();
        }
        UserRoleAuthInfoVO roleAuthInfoVO = AuthRoleMan.getUserRoleAuthRoleByUsername(username, type);
        if (roleAuthInfoVO == null) {
            return AjaxResult.errorResult("未获取到用户角色和权限信息");
        }
        return AjaxResult.successResult(roleAuthInfoVO);
    }
}
